/**
 * Create by richliu1023
 * @date 2016-06-07
 * @email richliu1023@gmail.com
 * @github https://github.com/RichLiu1023
 * @description a simple explanation
 */
class BottomNoticeUI {

	private _noticeList:NoticeTxtUI[] = [];
	private static MAX_VALUE:number = 3;
	private static GAP_VALUE:number = 74;
	private static TIME:number = 500;
	private _position:egret.Point;

	public constructor( x:number, y:number ) {
		this._position = new egret.Point( x, y );
	}

	public execute( txt:string, parent:egret.DisplayObjectContainer ):void {
		if ( !parent )return egret.log( 'error: TopNoticeUI Parent Is Null!' );
		var textField = this.getTextField( txt );
		parent.addChild( textField );
		this.startAction( textField );
	}

	private getTextField( txt ):NoticeTxtUI {
		var textField;
		if ( this._noticeList.length >= BottomNoticeUI.MAX_VALUE ) {
			textField = this._noticeList.shift();
			Tween.removeTweens( textField );
			textField.parent && textField.parent.removeChild( textField );
			textField.clear();
			this._noticeList.push( textField );
		}
		else {
			textField = new NoticeTxtUI();
			this._noticeList.push( textField );
		}
		textField.show( txt );
		return textField;
	}

	private startAction( textField:NoticeTxtUI ):void {
		Tween.removeTweens( textField );
		this.updatePosition();
		textField.alpha = 1;
		Tween.get( textField )
			.wait( BottomNoticeUI.TIME )
			.to( { alpha: 0.2 }, 1000 )
			.call( this.complete, this, [ textField ] );
	}

	private complete( textField:NoticeTxtUI ):void {
		textField.parent && textField.parent.removeChild( textField );
		textField.clear();
	}

	private updatePosition():void {
		var len:number = this._noticeList.length;
		var value:number = this._position.y;
		while ( len-- > 0 ) {
			this._noticeList[ len ].y = value;
			this._noticeList[ len ].x = this._position.x;
			value -= BottomNoticeUI.GAP_VALUE;
		}
	}
}